<?php
include_once './header/header.php';
include_once './mysql_con/connect.php';
$urlpath = substr(@$_SERVER["PATH_INFO"], 1);
if(!$urlpath){
	die("wrong url");
}

switch ($urlpath) {
	case 'GetAll':
		//获取所有文章
		getAll($pdo);
		break;
	case 'GetAllTittle':
		//获取所有文章标题
		getAllTittle($pdo);
		break;
	case 'GetOneArticle':
		//获取所有文章标题
		getOneArticle($pdo);
		break;
	case 'GetList':
		//获取文章列表（分页 page 第几页默认1 size 分页大小默认10）
		break;
	case 'GetSingle':
		//查询单个账户
		break;
	case 'Delete':
		//删除功能（文章）
		artDelete($pdo);
		break;
	case 'Insert':
		//插入功能（文章）
		insert($pdo);
		break;
	case 'Update':
		//更新功能（文章）
		artUpdate($pdo);
		break;
	case 'Login':
		//登陆功能
		login($pdo);
		break;
	case 'Register':
		//注册功能
		register($pdo);
		break;
	case 'CheckUser':
		//注册功能
		checkUser($pdo);
		break;
	case 'ArticleCount':
		articleCount($pdo);
		break;
	default:
		echo "没有正确的请求";
		break;
}
//获取所有文章
function getAll($pdoObject){
	$sql = "select * from articles";
	$stmt = $pdoObject->prepare($sql);
	$stmt->execute();
	$arr = $stmt->fetchAll();
	$res = [
		"status"=>true,
		"message"=>"成功查询".count($arr)."条数据",
		"data"=>$arr
	];
	echo json_encode($res);
}
//查询所有标题的功能
function getAllTittle($pdoObject){
	$sql = "select show_id,title,type from articles";
	$stmt = $pdoObject->prepare($sql);
	$stmt->execute();
	$arr = $stmt->fetchAll();
	$res = [
		"status"=>true,
		"message"=>"成功查询".count($arr)."条数据",
		"data"=>$arr
	];
	echo json_encode($res);
}
//查询一片文字的功能
function getOneArticle($pdoObject) {
	if(@$_GET["show_id"]==""){
		$res = [
		"status"=>false,
		"message"=>"请求失败，参数信息不完整",
		"data"=>0,
		"type"=>3
		];
		die (json_encode($res));
	}
	$sql = "select * from articles where show_id=?";
	$stmt = $pdoObject->prepare($sql);
	$stmt->execute([$_GET["show_id"]]);
	$arr = $stmt->fetchAll();
	$res = [
		"status"=>true,
		"message"=>"成功查询".count($arr)."条数据",
		"data"=>$arr
	];
	echo json_encode($res);
}
//注册的功能
function register($pdoObject){
	if(@$_POST["username"]==""||@$_POST["password"]==""||@$_POST["tell"]==""){
		$res = [
		"status"=>false,
		"message"=>"注册失败，信息不完全",
		"data"=>0,
		"type"=>3
		];
		die (json_encode($res));
	}
	$sql = "insert into userinfo values (null,?,?,?);";
	$stmt = $pdoObject->prepare($sql);
	$r = $stmt->execute([
		$_POST["username"],$_POST["password"],$_POST["tell"]
	]);
	if($stmt->rowCount()>0){
		$res = [
		"status"=>true,
		"message"=>"注册成功",
		"data"=>0,
		"type"=>1
		];
		echo json_encode($res);
	}else {
		$res = [
		"status"=>false,
		"message"=>"注册失败，账号或密码重复",
		"data"=>0,
		"type"=>2
		];
		echo json_encode($res);
	}
}
//登陆的功能
function login($pdoObject) {
	if(@$_POST["tell"]==""||@$_POST["password"]==""){
		$res = [
		"status"=>false,
		"message"=>"登陆失败，信息不完全",
		"data"=>0,
		"type"=>3
		];
		die (json_encode($res));
	}
	$sql = "select * from userinfo where tel=? and password=?;";
	$stmt = $pdoObject->prepare($sql);
	$stmt->execute([$_POST["tell"],$_POST["password"]]);
	$res = $stmt->fetchAll();
	if(count($res)>0){
		//token密码值；
		$token = md5("666".$_POST["tell"].$_POST["password"]);
		$res = [
		"status"=>true,
		"message"=>"登陆成功",
		"data"=>0,
		"type"=>1,
		"token"=>$token,
		"user"=>$res[0]["username"],
		"tell"=>$res[0]["tel"]
		];
		echo json_encode($res);
	}else {
		$res = [
		"status"=>false,
		"message"=>"登陆失败，账号或密码不正确",
		"data"=>0,
		"type"=>2
		];
		echo json_encode($res);
	}
}
//校验用户
function checkUser($pdo) {
	if(@$_POST["tell"]==""||@$_POST["token"]==""){
		$res = [
		"status"=>false,
		"message"=>"校验失败，信息不完全",
		"data"=>0,
		"type"=>3
		];
		die (json_encode($res));
	}
	$sql = "select * from userinfo where tel=?;";
	$stmt = $pdo->prepare($sql);
	$stmt->execute([$_POST["tell"]]);
	$res = $stmt->fetchAll();
	if(count($res)>0){
		//token密码值；
		$p = $res[0]["password"];
		$token = md5("666".$_POST["tell"].$p);
		if ($token == $_POST["token"]) {
			//真实用户
			$res = [
			"status"=>true,
			"message"=>"合法用户",
			"data"=>0,
			"type"=>1
			];
			echo json_encode($res);
		} else {
			//非法用户
			$res = [
			"status"=>false,
			"message"=>"非法用户",
			"data"=>0,
			"type"=>4
			];
			echo json_encode($res);
		}
	}else {
		$res = [
		"status"=>false,
		"message"=>"该电话用户不存在",
		"data"=>0,
		"type"=>2
		];
		echo json_encode($res);
	}
}
//文字统计
function articleCount($pdo) {
	$sql = "SELECT
	Count(articles.show_id),
	DATE_FORMAT( datetime, '%Y%m%d' )
	FROM
	articles
	GROUP BY
	DATE_FORMAT( datetime, '%Y%m%d' )
	ORDER BY
	articles.datetime DESC
	LIMIT 5;";
	$stmt = $pdo->prepare($sql);
	$stmt->execute();
	$res = $stmt->fetchAll();
	if (count($res)>0) {
		$time = [];
		for ($i=0; $i < count($res); $i++) { 
			array_push($time,$res[$i][1]);
		}
		$count = [];
		for ($i=0; $i < count($res); $i++) {
			array_push($count,$res[$i][0]);
		}
		$res = [
		"status"=>true,
		"message"=>"最近发布信息的5个工作日的统计数据",
		"data"=>[
			"time"=>$time,
			"count"=>$count
		],
		"type"=>2
		];
		echo json_encode($res);
	} else {
		$res = [
		"status"=>false,
		"message"=>"数据统计出现问题",
		"data"=>0,
		"type"=>1
		];
		echo json_encode($res);
	}
}
//插入新文章
function insert($pdo) {
	$sql = "insert into articles values (null,?,?,?,?,?,?)";
	$stmt = $pdo->prepare($sql);
	date_default_timezone_set( 'Asia/Shanghai' );
	$time = date("Y-m-d H:i:s");
	$show_id = md5($time);
	$stmt->execute([$show_id,$_POST['title'],$_POST['type']
	,$_POST['context'],$_POST['img_src'],$time]);
	if ($stmt->rowCount()>0) {
		$res = [
		"status"=>true,
		"message"=>"数据写入成功",
		"data"=>0,
		"type"=>0
		];
		echo json_encode($res);
	} else {
		$res = [
		"status"=>false,
		"message"=>"数据写入失败",
		"data"=>0,
		"type"=>1
		];
	}
}
//删除文章
function artDelete($pdo) {
	if(@$_POST["show_id"]==""){
		$res = [
		"status"=>false,
		"message"=>"删除失败，信息不完全",
		"data"=>0,
		"type"=>3
		];
		die (json_encode($res));
	}
	$sql = "delete from articles where show_id = ?;";
	$stmt = $pdo->prepare($sql);
	$r = $stmt->execute([$_POST["show_id"]]);
	if($stmt->rowCount()>0){
		$res = [
		"status"=>true,
		"message"=>"删除成功",
		"data"=>0,
		"type"=>1
		];
		echo json_encode($res);
	}else {
		$res = [
		"status"=>false,
		"message"=>"删除失败",
		"data"=>0,
		"type"=>2
		];
		echo json_encode($res);
	}
}

//更新文章的功能
function artUpdate($pdo) {
	if(@$_POST["show_id"]==""){
		$res = [
		"status"=>false,
		"message"=>"信息不完全，无法更新",
		"data"=>0,
		"type"=>3
		];
		die (json_encode($res));
	}
	$sql = "update articles SET title=?,context=?,type=?,datetime=? WHERE show_id=?";
	$stmt = $pdo->prepare($sql);
	date_default_timezone_set( 'Asia/Shanghai' );
	$time = date("Y-m-d H:i:s");
	$r = $stmt->execute([
		$_POST["title"],$_POST["context"],$_POST["type"],$time,$_POST["show_id"]
		]);
	if($stmt->rowCount()>0){
		$res = [
		"status"=>true,
		"message"=>"更新成功",
		"data"=>0,
		"type"=>1
		];
		echo json_encode($res);
	}else {
		$res = [
		"status"=>false,
		"message"=>"更新失败",
		"data"=>0,
		"type"=>2
		];
		echo json_encode($res);
	}
}
?>